<?php
declare(strict_types=1);

namespace app\job;

use app\service\NotificationService;
use think\queue\Job;
use think\facade\Log;

class SendNotificationJob
{
    public function fire(Job $job, $data)
    {
        try {
            Log::info('开始处理订单状态变更通知任务', [
                'order_no' => $data['order']->order_no,
                'status' => $data['status'],
                'attempt' => $job->attempts()
            ]);
            
            $notificationService = new NotificationService();
            $notificationService->sendOrderStatusNotification($data['order'], $data['status']);
            
            Log::info('订单状态变更通知任务处理成功', [
                'order_no' => $data['order']->order_no,
                'status' => $data['status']
            ]);
            
            $job->delete();
        } catch (\Exception $e) {
            Log::error('订单状态变更通知任务处理失败', [
                'order_no' => $data['order']->order_no,
                'status' => $data['status'],
                'attempt' => $job->attempts(),
                'error' => $e->getMessage()
            ]);
            
            if ($job->attempts() > 3) {
                Log::warning('订单状态变更通知任务重试次数超限，放弃处理', [
                    'order_no' => $data['order']->order_no,
                    'status' => $data['status']
                ]);
                $job->delete();
            } else {
                $job->release(3);
            }
        }
    }
} 